//
//  AddressTool.m
//  体育商城
//
//  Created by  on 2022/9/24.
//

#import "AddressTool.h"

@implementation AddressTool
+ (void)find:(NSString *)account complete:(nullable void(^) (NSArray *result))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"account"),bg_sqlValue(account)];
    [Address bg_findAsync:nil where:where complete:^(NSArray * _Nullable array) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block) {block(array);}
        });
    }];
}

+ (void)delete:(Address *)address complete:(nullable void(^) (void))block{
    if (address.selected == NO) {
        return;
    }
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"account"),bg_sqlValue(address.account),bg_sqlKey(@"ID"),bg_sqlValue(address.ID)];
    [Address bg_deleteAsync:nil where:where complete:^(BOOL isSuccess) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block && isSuccess) {block();}
        });
    }];
}

+ (void)update:(NSArray <Address *>*)array complete:(nullable void(^) (void))block{
    [Address bg_saveOrUpdateArrayAsync:array complete:^(BOOL isSuccess) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block && isSuccess) {block();}
        });
    }];
}
+ (NSInteger)count:(NSString *)account{
    NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"account"),bg_sqlValue(account)];
    return [Address bg_count:nil where:where];
}
@end
